﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.popups.sendgift {
    import reprise.ui.*;
    import flash.events.*;
    import reprise.external.*;
    import flash.display.*;
    import reprise.controls.*;
    import flash.utils.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.popups.*;
    import net.wooga.pets.constants.*;
    import net.wooga.pets.utils.*;
    import net.wooga.pets.external.*;

    public class GiftPopupMediator extends AbstractPopupMediator {

        public var friendsModel:FriendsModel;
        public var userModel:UserModel;
        private var _giftButtonGroup:RadioButtonGroup;
        private var _smallItemsDisplay:UIComponent;
        private var _baby:MovieClip;
        private var _timeOutID:uint;

        private static const ICON_WIDTH:Number = 50;
        private static const ITEMCONTAINER_WIDTH:Number = 80;

        override public function onRegister():void{
            super.onRegister();
            this.createGiftBaby();
            addOkButton("global_button_sendgift");
            addLabel("title showcardGothicFont", "gifts_choose_friends_headline");
            this._smallItemsDisplay = RepriseUtils.addComponent(this.view, "smallItemsDisplay");
            if (this.isSendGiftAtStartPopup()){
                this.createSendGiftAtStartPopup();
                this.view.addCSSClass("atStartPopup");
            } else {
                this.createSendGiftInGamePopup();
                this.view.addCSSClass("inGamePopup");
            };
            this.addFriend();
            this.selectFirstEnabledItem();
        }
        private function selectFirstEnabledItem():void{
            var _local2:RadioButton;
            var _local1:Array = this._smallItemsDisplay.children();
            for each (_local2 in _local1) {
                if (_local2.enabled){
                    _local2.selected = true;
                    break;
                };
            };
        }
        private function isSendGiftAtStartPopup():Boolean{
            return ((this.view.data.length == 3));
        }
        private function createGiftBaby():void{
            this._baby = AssetsLibrary.getMovieClip("GiftGiraffeBaby");
            this._baby.stop();
            this._baby.visible = false;
            this._baby.y = (this._baby.y - 90);
            this._baby.x = 100;
            RepriseUtils.addClipComponent(this.view, "baby", this._baby);
        }
        private function createSendGiftInGamePopup():void{
            this.addItems();
            addLabel("orLabel myriadProFont", "global_or");
            addLabel("selectLabel myriadProFontNormal", "gift_popup_select_item");
        }
        private function createSendGiftAtStartPopup():void{
            this.addItems(this._smallItemsDisplay);
        }
        private function addItems(_arg1:UIComponent=null):void{
            var _local2:String;
            var _local3:String;
            var _local4:RadioButton;
            var _local5:Array = (this.view.data as Array);
            var _local6:int;
            while (_local6 < _local5.length) {
                _local3 = _local5[_local6].amount;
                _local2 = _local5[_local6].type;
                if (_local2 == GameConstants.GIFT_SURPISE_PACKAGE){
                    this.addSurprisePackage(_local5[_local6].amount, _local5[_local6].type, _arg1);
                } else {
                    if (_local2 == GameConstants.GIFT_SURPRISE_PLACEHOLDER){
                        this.addSurprisePlaceholder(_arg1);
                    } else {
                        _local4 = this.addGiftItem(_local5[_local6].amount, _local5[_local6].type);
                    };
                };
                _local6++;
            };
            this._giftButtonGroup = _local4.group();
            this._giftButtonGroup.addEventListener(Event.CHANGE, this.giftButtonGroup_change);
        }
        private function giftButtonGroup_change(_arg1:Event):void{
            var selectedButton:UIComponent;
            var event = _arg1;
            selectedButton = this._giftButtonGroup.selectedRadioButton();
            var newBabyX:Number = (((selectedButton.x + (selectedButton.width / 2)) + this._smallItemsDisplay.x) + 10);
            if (this._baby.x == newBabyX){
                return;
            };
            this._baby.gotoAndPlay("hide");
            this._timeOutID = setTimeout(function ():void{
                _baby.visible = true;
                _baby.x = (((selectedButton.x + (selectedButton.width / 2)) + _smallItemsDisplay.x) + 10);
                _baby.gotoAndPlay("appear");
            }, 600);
        }
        private function addSurprisePlaceholder(_arg1:UIComponent=null):void{
            var _local2:RadioButton = RepriseUtils.addRadioButton(((_arg1) || (this.view)), "item surpriseItem", "giftGroup");
            _local2.enabled = false;
            RepriseUtils.addLabel(_local2, "emptyItemLabel myriadProFontNormal", language.getTextByKey("inventory_slot_comingsoon"));
        }
        private function addSurprisePackage(_arg1:int, _arg2:String, _arg3:UIComponent=null):void{
            var _local4:RadioButton = RepriseUtils.addRadioButton(((_arg3) || (this.view)), "item surpriseItem", "giftGroup");
            _local4.setData({type:_arg2, amount:_arg1});
            RepriseUtils.addComponent(_local4, "surprisePackage");
            addLabel("sendSurpriseLabel myriadProFontNormal", "gift_popup_send_surprise");
            addLabel("surpriseDescriptionLabel myriadProFontNormal", "gift_popup_gift_surprise_package");
        }
        public function addGiftItem(_arg1:int, _arg2:String):RadioButton{
            var _local4:UIComponent;
            var _local5:String;
            var _local3:RadioButton = RepriseUtils.addRadioButton(this._smallItemsDisplay, "item", "giftGroup");
            _local3.setData({type:_arg2, amount:_arg1});
            if (_arg2 == GameConstants.GIFT_ITEM_PLACEHOLDER){
                RepriseUtils.addComponent(_local3, "icon");
                RepriseUtils.addLabel(_local3, "emptyItemLabel myriadProFontNormal", language.getTextByKey("inventory_slot_comingsoon"));
                _local3.enabled = false;
            } else {
                _local4 = RepriseUtils.addComponent(_local3, (_arg2 + "Icon icon"));
                RepriseUtils.validateComponent(_local4, this.onIconAdded);
                _local5 = language.getTextByKey(("global_amount_" + _arg2), {amount:_arg1});
                RepriseUtils.addLabel(_local3, "itemLabel myriadProFontNormal", _local5);
            };
            return (_local3);
        }
        private function onIconAdded(_arg1:UIComponent):void{
            Displays.correctScaling(_arg1, _arg1, ICON_WIDTH, ICON_WIDTH, -1, 0);
            _arg1.setStyle("left", String(((ITEMCONTAINER_WIDTH / 2) - ((_arg1.borderBoxWidth * _arg1.scaleX) / 2))));
        }
        protected function addFriend():void{
            var _local3:int;
            var _local6:int;
            var _local1:UIComponent = RepriseUtils.addComponent(this.view, "friend");
            var _local2:Object = new Object();
            if (this.friendsModel.friends.length <= 2){
                _local6 = 0;
                while (_local6 < this.friendsModel.friends.length) {
                    if (this.friendsModel.friends[_local6].uid == GameConstants.KARLA_FACEBOOK_ID){
                        _local2 = this.friendsModel.friends[_local6];
                    };
                    _local6++;
                };
            } else {
                do  {
                    _local3 = (Math.random() * this.friendsModel.friends.length);
                    _local2 = this.friendsModel.friends[_local3];
                } while ((((_local2.uid == this.userModel.mySocialID)) || ((_local2.uid == GameConstants.KARLA_FACEBOOK_ID))));
            };
            var _local4:String = ((_local2.picture) || (GameConstants.FACEBOOK_DEFAULT_PIC));
            var _local5:FacebookImageResource = new FacebookImageResource(_local4);
            _local5.addEventListener(Event.COMPLETE, this.imageResourceComplete);
            _local5.execute();
            RepriseUtils.addClipComponent(_local1, "image", _local5.loader());
            RepriseUtils.addLabel(_local1, "myriadProFont label", _local2.first_name);
        }
        private function imageResourceComplete(_arg1:Event):void{
            var _local2:ImageResource = ImageResource(_arg1.target);
            _local2.removeEventListener(Event.COMPLETE, this.imageResourceComplete);
        }
        private function get view():GiftPopupView{
            return ((viewComponent as GiftPopupView));
        }
        override protected function onOk(_arg1:MouseEvent):void{
            var _local2:Object = this._giftButtonGroup.selectedRadioButton().data();
            var _local3:Object = {request_type:_local2.type, is_gift:true, entity_id:null};
            dispatch(new InfoEvent(EventConstants.SEND_GIFT_REQUEST, null, _local3));
            this.close();
        }
        override protected function close():void{
            clearTimeout(this._timeOutID);
            super.close();
        }

    }
}//package net.wooga.pets.views.popups.sendgift 
